package queue

import (
	"algorithms/array"
	"fmt"
	"strings"
)

// ArrayQueue 数组队列
type ArrayQueue[E any] struct {
	array *array.Array[E]
}

func MakeArrayQueue0[E any](capacity int) *ArrayQueue[E] {
	return &ArrayQueue[E]{
		array: array.MakeArray0[E](capacity),
	}
}

func MakeArrayQueue[E any]() *ArrayQueue[E] {
	return MakeArrayQueue0[E](10)
}

// GetSize 查看队列元素数量
func (a *ArrayQueue[E]) GetSize() int {
	return a.array.GetSize()
}

// GetCap 查看队列容量
func (a *ArrayQueue[E]) GetCap() int {
	return a.array.GetCap()
}

// DeEnqueue 队首出队
func (a *ArrayQueue[E]) DeEnqueue() (E, error) {
	return a.array.RemoveFirst()
}

// Enqueue 队尾入队
func (a *ArrayQueue[E]) Enqueue(e E) error {
	return a.array.AddLast(e)
}

// GetFront 查看队首元素
func (a *ArrayQueue[E]) GetFront() (E, error) {
	return a.array.GetFirst()
}

//IsEmpty 队列是否为空
func (a *ArrayQueue[E]) IsEmpty() bool {
	return a.array.IsEmpty()
}

func (a *ArrayQueue[E]) String() string {
	var sb strings.Builder
	sb.WriteString("Queue: ")
	sb.WriteString("front [")
	for i := 0; i < a.array.GetSize(); i++ {
		e, _ := a.array.Get(i)
		sb.WriteString(fmt.Sprintf("%+v", e))
		if i != a.array.GetSize()-1 {
			sb.WriteString(", ")
		}
	}
	sb.WriteString("] tail")
	return sb.String()
}
